package indi.zn.commons.simplepoi;

import java.util.Collections;
import java.util.List;

import com.google.common.collect.Lists;

public class WorkbookStyle {

	private List<LineEntry> rowList;
	private List<LineEntry> columList;

	private List<CellStyle> cellStyleList; // 外层 y 里层 x

	private int fastPiont = 0;

	public List<CellStyle> getCellStyleList() {
		return cellStyleList;
	}

	public void setCellStyleList(List<CellStyle> cellStyleList) {
		this.cellStyleList = cellStyleList;
	}

	public WorkbookStyle() {

		rowList = Lists.<LineEntry> newArrayList();
		columList = Lists.<LineEntry> newArrayList();
		cellStyleList = Lists.<CellStyle> newArrayList();

	}

	/**
	 * 
	 * @param x
	 *            -1 为 整行 (-1,5) 第五行
	 * @param y
	 *            -1 为整列 (1 ,-1 ) 第一列
	 * @param style
	 */
	public void addCellStyle(CellStyle style) {

		cellStyleList.add(style);

	}

	public CellStyle getCellStyle(int x, int y) {

		CellStyle rs = null;
		
		if(cellStyleList.size()==0)return null;

		for (int i = fastPiont; i < cellStyleList.size(); i++) {

			CellStyle cs = cellStyleList.get(i);


			int m = cs.match(x, y);

			if (m == 0) {

				rs = cs;

				fastPiont = i;
				break;
			} else if (m == -1) {

				continue;

			} else if (m == 1) {

				break;

			}

		}

		if (rs == null) {
			for (int i = fastPiont; i >= 0; --i) {
				CellStyle cs = cellStyleList.get(i);
				
				int m = cs.match(x, y);

				if (m == 0) {

					rs = cs;

					// fastPiont=i;
					break;
				} else if (m == -1) {

					continue;
				} else if (m == 1) {

					continue;

				}

			}

		}

		return rs;

	}

	/**
	 * @param hight
	 *            EXCEL列高度的单位是磅,Apache POI的行高度单位是缇(twip)： 1英寸=72磅=25.4毫米=1440缇
	 *            1磅=0.353毫米=20缇 POI中的行高＝Excel的行高度*20 Excel的行高度=POI中的行高/20 Excel
	 *            默认 13.5 磅 POI= 270
	 * @param rownum
	 *            -1 为所有行
	 * 
	 */
	public void setRowHight(int rownum, short hight) {

		rowList.add(new LineEntry(rownum, hight));

	}

	public Short getRowHight(int rownum) {

		int index = -1;

		if (rowList.size() > 0 && rowList.get(0).getNum() == -1) {

			index = 0;
		}else{

		index = Collections.binarySearch(rowList, new LineEntry(rownum));
		}
		
		if (index < 0 ) {
			return null;
		}

		return rowList.get(index).getValue();

	}

	/**
	 * POI中的字符宽度算法是： double 宽度 = (字符个数 * (字符宽度 - 1) + 5) / (字符宽度 - 1) * 256
	 * 然后再四舍五入成整数
	 * 
	 * @param colum
	 *            -1 为所有列
	 * @param hight
	 *            n * 256
	 */
	public void setColumWidth(int colum, short width) {

		columList.add(new LineEntry(colum, width));

	}

	public Short getColumHight(int columnum) {

		int index = -1;

		if (columList.size() > 0 && columList.get(0).getNum() == -1) {

			index = 0;
		}

		index = Collections.binarySearch(columList, new LineEntry(columnum));

		if (index == -1) {
			return null;
		}

		return columList.get(index).getValue();

	}

	/**
	 * 内部排序
	 */
	public void tidy() {

		Collections.sort(rowList);
		Collections.sort(columList);

		Collections.sort(cellStyleList);

	}

	static class LineEntry implements Comparable<LineEntry> {

		private int num;
		private short value;

		public LineEntry(int num) {
			this.num = num;

		}

		public LineEntry(int num, short value) {
			// TODO Auto-generated constructor stub

			this.num = num;
			this.value = value;

		}

		public int getNum() {
			return num;
		}

		public void setNum(int num) {
			this.num = num;
		}

		public short getValue() {
			return value;
		}

		public void setValue(short value) {
			this.value = value;
		}

		@Override
		public int compareTo(LineEntry o) {
			// TODO Auto-generated method stub

			if (this.num > o.num)
				return 1;
			if (this.num < o.num)
				return -1;

			return 0;
		}

	}

}
